import Vue from 'vue'
import Router from 'vue-router'

const Login = () => import(/* webpackChunkName:"login_home_welcome" */ './components/Login.vue')
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ './components/Home.vue')
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ './components/Welcome.vue')
// import Login from './components/Login.vue'
// import Home from './components/Home.vue'
// import Welcome from './components/Welcome.vue'
const User = () => import(/* webpackChunkName:"user" */ './components/users/User.vue')
// import User from './components/users/User.vue'
const Rights = () => import(/* webpackChunkName:"rights_roles" */ './components/right/Rights.vue')
// import Rights from './components/right/Rights.vue'
const Roles = () => import(/* webpackChunkName:"rights_roles" */ './components/right/Roles.vue')
// import Roles from './components/right/Roles.vue'

const Cate = () => import(/* webpackChunkName:"cate-params-goods-add" */ './components/goods/Cate.vue')
// import Cate from './components/goods/Cate.vue'
const GoodsParams = () => import(/* webpackChunkName:"cate-params-goods-add" */ './components/goods/GoodsParams.vue')
// import GoodsParams from './components/goods/GoodsParams.vue'
const Goods = () => import(/* webpackChunkName:"cate-params-goods-add" */ './components/goods/Goods.vue')
// import Goods from './components/goods/Goods.vue'
const AddGoods = () => import(/* webpackChunkName:"cate-params-goods-add" */ './components/goods/AddGoods.vue')
// import AddGoods from './components/goods/AddGoods.vue'
const Orders = () => import(/* webpackChunkName:"order" */ './components/order/Orders.vue')
// import Orders from './components/order/Orders.vue'
const Echarts = () => import(/* webpackChunkName:"echart" */ './components/echarts/Echarts.vue')
// import Echarts from './components/echarts/Echarts.vue'

Vue.use(Router)

var router = new Router({
  routes: [{
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    }, {
      path: '/home',
      redirect:'/welcome',
      component: Home,
      children:[{
        path:'/welcome',
        component:Welcome
      },{
        path:'/users',
        component:User
      },{
        path:"/rights",
        component:Rights
      },{
        path:"/roles",
        component:Roles
      },{
        path:'/categories',
        component:Cate
      },{
        path:'/params',
        component:GoodsParams
      },{
        path:'/goods',
        component:Goods
      },{
        path:'/add',
        component:AddGoods
      },{
        path:'/orders',
        component:Orders
      },{
        path:'/reports',
        component:Echarts
      }]
    }
  ]
})
// 通过路由导航守卫控制页面访问权限
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    // to:要去的页面,from:从哪个页面来的,next:执行后续的代码
    next();
  } else {
    if (sessionStorage.getItem('token')) {
      next(); //放行
    } else {
      next('/login');  //没有登录token,返回登录页
    }
  }
})
export default router;